import datetime
import os
from urllib import parse


class BaseConfig(object):
    PORT = 5000
    APP_NAME = "flask-app"
    JSON_AS_ASCII = False  # 禁止中文转义
    # 因为flask的session是通过加密之后放到了cookie中。所以有加密就有密钥用于解密，所以，只要用到了flask的session模块就一定要配置“SECRET_KEY”这个全局宏。一般设置为24位的字符
    SECRET_KEY = os.urandom(24)
    # 权限白名单列表
    WHITE_LIST = ["/sys/user/login", "/callback/gitee"]
    # 环境变量中获取DB_NAME
    DB_NAME = os.getenv("DB_NAME")
    if DB_NAME is None:
        DB_NAME = "flaskrbac"
    # 环境变量中获取数据库DB_HOST
    DB_HOST = os.getenv("DB_HOST", "172.16.31.160")
    # 环境变量中获取数据库DB_PORT
    DB_PORT = os.getenv("DB_PORT", "3306")
    # 环境变量中获取数据库DB_USER
    DB_USER = os.getenv("DB_USER", "root")
    # 环境变量中获取数据库DB_PASSWORD
    DB_PASSWORD = os.getenv("DB_PASSWORD", "8Eli#gr#AUk")
    # 环境变量中获取数据库REDIS_HOST
    REDIS_HOST = os.getenv("REDIS_HOST", "172.16.31.160")
    # 环境变量中获取数据库REDIS_PORT
    REDIS_PORT = os.getenv("REDIS_PORT", "6379")
    # 环境变量中获取数据库REDIS_PASSWORD
    REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", "gEwjEHLM")
    # mysql+pymysql://root:xxxxx@172.16.31.160/flaskrbac?charset=utf8mb4
    SQLALCHEMY_DATABASE_URI = f'mysql+pymysql://{DB_USER}:{parse.quote_plus(DB_PASSWORD)}@{DB_HOST}:{DB_PORT}/{DB_NAME}?charset=utf8mb4'
    PERMANENT_SESSION_LIFETIME = datetime.timedelta(days=90)
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    SQLALCHEMY_RECORD_QUERIES = True
    SQLALCHEMY_ENGINE_OPTIONS = {
        'pool_size': 5,
        'pool_timeout': 90,
        'pool_recycle': 7200,
        'max_overflow': 1024
    }
    # REDIS_URL = "redis://:password@localhost:6379/0"
    REDIS_URL = f'redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/0'
    # 回调配置
    # 回调密码
    GITEE_CALLBACK_PASSWORD = os.getenv("GITEE_CALLBACK_PASSWORD", "666")
    # 回调执行的脚本
    GITEE_CALLBACK_SHELL = os.getenv("GITEE_CALLBACK_SHELL", "/home/admin/app/restart.sh")

class Development(BaseConfig):
    PORT = 5000
    DEBUG = True
    ENV = "dev"
    TESTING = True
    SQLALCHEMY_ECHO = True  # 打印SQL


class Test(BaseConfig):
    PORT = 5000
    DEBUG = True
    ENV = "test"
    TESTING = True


class Production(BaseConfig):
    PORT = 5000
    DEBUG = False
    ENV = "prod"
    TESTING = False


config = {
    "dev": Development,
    "development": Development,
    "test": Test,
    "prod": Production,
    "production": Production
}
